/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.controls;

import com.phidgets.PhidgetException;
import java.util.ArrayList;
import wwcontrolserver.datarecorders.AbstractDataRecorder;
import wwcontrolserver.datarecorders.WWControlServerLogger;
import wwcontrolserver.ifk.AbstractIFK;

/**
 *
 * @author Michael Ferry
 */
public class DataCollectionControl extends AbstractControl {
    protected OnOffControlState CurrentState;
    protected ArrayList<AbstractDataRecorder> DataRecorders;
    protected ArrayList<AbstractIFK> IFKarray;
    
    public DataCollectionControl(OnOffControlState InitialState, WWControlServerLogger WWCSLog,
            ArrayList<AbstractDataRecorder> DataRecorders, ArrayList<AbstractIFK> IFKarray){
        super(InitialState,WWCSLog);
        
        this.CurrentState = InitialState;
        this.DataRecorders = DataRecorders;
        this.IFKarray = IFKarray;
    }
    
    @Override
    public void NotifyControl(){
        String NotificationString = "Control Notified " + this.GetControlName();
        System.out.println(NotificationString);
        WWCSLog.RecordLogEntry(NotificationString);
        CarryOutDataCollection();    
    }
    
    protected void CarryOutDataCollection() {
        if (CurrentState.ControlActivation) {
            try {
                for (AbstractIFK ifkToProcess : IFKarray) {
                    ifkToProcess.RecordIFKState(WWCSLog, DataRecorders);
                }
            } catch (PhidgetException Phe) {
                WWCSLog.RecordExceptionLogEntry(Phe);
            }
        }
    }
    @Override
    public AbstractControlState GetControlState(){
        return CurrentState;
    }
    @Override
    public void SetControlState(AbstractControlState UpdatedState) throws Exception{
        CurrentState.UpdateControlState(UpdatedState);
        if(CurrentState.ControlActivation){
            String OutString = "Control Active " + this.GetControlName();
            System.out.println(OutString);
            WWCSLog.RecordLogEntry(OutString);
        }else{
            String OutString = "Control Inactive " + this.GetControlName();
            System.out.println(OutString);
            WWCSLog.RecordLogEntry(OutString);
        }
    }
    @Override
    public ArrayList<ControlKey> GetListOfControlKeys(){
        return null;
    }
    @Override
    public void ShutdownControl() {
        CurrentState.SetOffState();
        //UpdateDataCollectionControlState();
//        try {
//            UpdateDutyCycleControlState();
//        } catch (Exception e) {
//            WWCSLog.RecordLogEntry(e.toString());
//        }
    }
}
